import {
    Get,
    Post,
    Body,
    Patch,
    Param,
    Delete,
    Query,
    ValidationPipe,
    HttpStatus,
    HttpCode,
    ParseUUIDPipe,
    ParseArrayPipe,
  } from '@nestjs/common';
  import { ApiOperation, ApiResponse, ApiQuery, ApiParam, ApiBody } from '@nestjs/swagger';
  import { BaseCrudService } from '../services/base-crud.service';
  import { BaseEntity } from '../entity/base.entity';
  import { PaginationDto } from '../dto/base.dto';
  
  export abstract class BaseCrudController<
    T extends BaseEntity,
    CreateDto,
    UpdateDto
  > {
    constructor(protected readonly service: BaseCrudService<T>) {}
  
    @Post()
    @HttpCode(HttpStatus.CREATED)
    @ApiOperation({ summary: '创建记录' })
    @ApiResponse({ status: 201, description: '创建成功' })
    @ApiResponse({ status: 400, description: '请求参数错误' })
    async create(@Body(ValidationPipe) createDto: CreateDto) {
      return await this.service.create(createDto as any);
    }
  
    @Get()
    @ApiOperation({ summary: '获取记录列表' })
    @ApiQuery({ name: 'page', required: false, description: '页码' })
    @ApiQuery({ name: 'pageSize', required: false, description: '每页数量' })
    @ApiQuery({ name: 'sortBy', required: false, description: '排序字段' })
    @ApiQuery({ name: 'sortOrder', required: false, description: '排序方向' })
    @ApiResponse({ status: 200, description: '查询成功' })
    async findAll(@Query(ValidationPipe) paginationDto: PaginationDto) {
      return await this.service.findAll(paginationDto);
    }
  
    @Get(':id')
    @ApiOperation({ summary: '根据ID获取记录' })
    @ApiParam({ name: 'id', description: '记录ID' })
    @ApiResponse({ status: 200, description: '查询成功' })
    @ApiResponse({ status: 404, description: '记录不存在' })
    async findOne(@Param('id', ParseUUIDPipe) id: string) {
      return await this.service.findOne(id);
    }
  
    @Patch(':id')
    @ApiOperation({ summary: '更新记录' })
    @ApiParam({ name: 'id', description: '记录ID' })
    @ApiResponse({ status: 200, description: '更新成功' })
    @ApiResponse({ status: 404, description: '记录不存在' })
    async update(
      @Param('id', ParseUUIDPipe) id: string,
      @Body(ValidationPipe) updateDto: UpdateDto,
    ) {
      return await this.service.update(id, updateDto as any);
    }
  
    @Delete(':id')
    @HttpCode(HttpStatus.NO_CONTENT)
    @ApiOperation({ summary: '删除记录' })
    @ApiParam({ name: 'id', description: '记录ID' })
    @ApiResponse({ status: 204, description: '删除成功' })
    @ApiResponse({ status: 404, description: '记录不存在' })
    async remove(@Param('id', ParseUUIDPipe) id: string) {
      return await this.service.remove(id);
    }
  
    @Delete('batch')
    @HttpCode(HttpStatus.NO_CONTENT)
    @ApiOperation({ summary: '批量删除记录' })
    @ApiResponse({ status: 204, description: '批量删除成功' })
    async batchRemove(
      @Body('ids', new ParseArrayPipe({ items: String })) ids: string[]
    ) {
      return await this.service.batchRemove(ids);
    }
  
    @Get('count/total')
    @ApiOperation({ summary: '统计记录总数' })
    @ApiResponse({ status: 200, description: '统计成功' })
    async count() {
      return await this.service.count();
    }
  
    @Get(':id/exists')
    @ApiOperation({ summary: '检查记录是否存在' })
    @ApiParam({ name: 'id', description: '记录ID' })
    @ApiResponse({ status: 200, description: '检查完成' })
    async exists(@Param('id', ParseUUIDPipe) id: string) {
      const exists = await this.service.exists(id);
      return {
        success: true,
        data: { exists },
        message: exists ? '记录存在' : '记录不存在'
      };
    }
  }